<?php

namespace App\Console\Commands;

use App\Http\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;

class sysnUser extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sysnUser';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $users = User::get();
        Config::set('database.connections.mysql.host', env('DB_HOST'));
        Config::set('database.connections.mysql.database', 'chatim');
        Config::set('database.connections.mysql.username', env('DB_USERNAME'));
        Config::set('database.connections.mysql.password', env('DB_PASSWORD'));
        DB::reconnect(); //重连数据库
        // 注册chatim项目的会员
        foreach ($users as $user) {
            $data['id']       = $user->id;
            $data['number']   = $user->id;
            $data['name']     = $user->id_wechat;
            $data['nickname'] = $user->id_wechat;
            $data['pwd']      = $user->password;
            $data['avatar']   = 'static/avatar/20190612152601.jpg';
            $item             = DB::table('app_user')->find($user->id);
            if (!$item) {
                DB::table('app_user')->insert($data);
            }
        }
    }
}
